package io.yizoo.business.controller;

import com.fasterxml.jackson.databind.node.ObjectNode;
import io.yizoo.business.mapper.biz_analysis_cockpitMapper;
import io.yizoo.business.entity.*;
import io.elm.core.CoreController;
import io.elm.core.HMapper;
import io.yizoo.system.entity.sys_org;
import io.yizoo.system.entity.sys_user;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/business/pmis/pj/show")
public class pmis_pj_showController extends CoreController {

    @Autowired
    private biz_analysis_cockpitMapper biz_analysis_cockpitMapper;
    @Autowired
    public io.yizoo.business.mapper.biz_pmis_pjMapper biz_pmis_pjMapper;

    @PostMapping("/GetContData")
    public Object GetContData(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .Select("t.*,a.name as charge_person_mc,b.name as org_mc,c.name as glsj_mc,d.kh_name as glxs_mc")
                        .From(biz_cmis_contracts.class,"t")
                        .LeftJoin(biz_hrm_ei_basic.class,"a","a.emp_code=t.charge_person")
                        .LeftJoin(sys_org.class,"b","b.id=t.org_id")
                        .LeftJoin(biz_crm_opportunity.class,"c","c.sys_code_key=t.glsj")
                        .LeftJoin(biz_crm_lead.class,"d","d.sys_code_key=t.glxs")
                        .Where("and t.sys_code_key =#{sys_code_key}","sys_code_key")
                        .GetFactor());
        return JsonResult(ret);
    }

    @PostMapping("/GetContZcData")
    public Object GetContZcData(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .Select("t.*,a.name as charge_person_mc,b.name as org_mc,c.name as glsj_mc,d.kh_name as glxs_mc")
                        .From(biz_cmis_contracts_zc.class,"t")
                        .LeftJoin(biz_hrm_ei_basic.class,"a","a.emp_code=t.charge_person")
                        .LeftJoin(sys_org.class,"b","b.id=t.org_id")
                        .LeftJoin(biz_crm_opportunity.class,"c","c.sys_code_key=t.glsj")
                        .LeftJoin(biz_crm_lead.class,"d","d.sys_code_key=t.glxs")
                        .Where("and t.glxm =#{glxm}","glxm")
                        .GetFactor());
        return JsonResult(ret);
    }

    @PostMapping("/GetTaskData")
    public Object GetTaskData(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .Select("t.*,a.name as assignee_mc")
                        .From(biz_pmis_pj_task.class,"t")
                        .LeftJoin(biz_hrm_ei_basic.class,"a","a.emp_code=t.assignee_id")
                        .Where("and t.pj_id =#{pj_id}","pj_id")
                        .OrderBy("t.assignee_id,t.jh_end_time")
                        .GetFactor());
        return JsonResult(ret);
    }

    @PostMapping("/GetCostList")
    public Object GetCostList(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .Select("a.col_1 AS item_type, a.col_2 AS invoice_type, a.col_4 AS cost_amount, b.col_6 as pj_id, b.sys_createtime, c.nick_name")
                        .From(fw_com_sub.class, "a")
                        .LeftJoin(biz_bms_reim.class,"b", "a.biz_id = b.id")
                        .LeftJoin(sys_user.class,"c", "b.sys_createuser = c.id")
                        .Where("and b.col_6 = #{pj_id}","pj_id")
                        .IsWhere("and YEAR ( b.sys_createtime ) = #{now_year}", "now_year")
                        .IsWhere("and (MONTH ( b.sys_createtime ) = #{now_month} or #{now_month} = 'total')", "now_month")
                        .IsWhere("and YEAR ( b.sys_createtime ) != #{not_year}", "not_year")
                        .IsWhere("and a.col_1 = #{type}", "type")
                        .OrderBy("b.sys_createtime desc")
                        .GetFactor());
        return JsonResult(ret);
    }

    @PostMapping("/GetIncomeList")
    public Object GetIncomeList(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .Select("a.*,b.name as sk_person_name")
                        .From(biz_bms_receipt.class, "a")
                        .LeftJoin(biz_hrm_ei_basic.class,"b", "b.emp_code = a.sk_person")
                        .Where("and a.pj_id =#{pj_id}","pj_id")
                        .IsWhere("and YEAR(a.sk_date) = #{now_year}", "now_year")
                        .IsWhere("and (MONTH(a.sk_date) = #{now_month} or #{now_month} = 'total') ", "now_month")
                        .OrderBy("a.sk_date desc")
                        .GetFactor());
        return JsonResult(ret);
    }

    @PostMapping("/GetTaskByStage")
    public Object GetTaskByStage(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .Select("t.*,a.name as assignee_mc")
                        .From(biz_pmis_pj_task.class,"t")
                        .LeftJoin(biz_hrm_ei_basic.class,"a","a.emp_code=t.assignee_id")
                        .Where("and t.stage =#{stage}","stage")
                        .OrderBy("t.assignee_id,t.jh_end_time")
                        .GetFactor());
        return JsonResult(ret);
    }

    @PostMapping("/GetInsStageData")
    public Object GetInsStageData(@RequestBody String jsonStr) {
        HMapper.PageData ret = Ctor.HMapper.GetTreePro(
                Ctor.DMaker(jsonStr).Selector
                        .From(biz_pmis_pj_ins_stage.class)
                        .Where("and pj_id=#{pj_id}","pj_id")
                        .GetFactor(), "stage_id", "pid"
        );
        if(ret.getSelectData().size()>0)
        {
            return JsonResult(ret);
        }
        else {
            biz_pmis_pjMapper.Initpj_ins_stage(
                    Ctor.DMaker(jsonStr).Selector.GetFactor()
            );
            return JsonResult(
                    Ctor.HMapper.GetTreePro(Ctor.DMaker(jsonStr).Selector
                            .From(biz_pmis_pj_ins_stage.class)
                            .Where("and pj_id=#{pj_id}","pj_id")
                            .GetFactor(), "stage_id", "pid")
            );
        }
    }

    @PostMapping("/GetData")
    public Object GetData(@RequestBody String jsonStr) {
        List<?> ret =  biz_pmis_pjMapper.GetFullData(
                Ctor.DMaker(jsonStr).Selector
                        .IsWhere("and t.id=#{pj_id}","pj_id")
                        .IsWhere("and t.name like  concat('%', #{name}, '%')","name")
                        .IsWhere("and t.org =#{org}","org")
                        .IsWhere("and t.manager=#{manager}","manager")
                        .IsWhere("and t.type=#{type}","type")
                        .IsPage()
                        .GetFactor()
        );
        return JsonResultPage(ret);
    }

    @PostMapping("/GetCostType")
    public Object GetCostType(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .From(biz_bms_cost_type.class)
                        .OrderBy("sort asc")
                        .GetFactor());
        return JsonResult(ret);
    }
}